﻿using Avalonia.BiliBili.Libs;
using Avalonia.BiliBili.Libs.Extensions;
using Avalonia.Controls;
using Avalonia.Threading;
using Bilibili.WebClient.Common;
using PCLUntils;
using PCLUntils.Plantform;
using System;

namespace Avalonia.BiliBili.Units
{
    public class Navigater : NavigaterBase
    {
        public static void OpenWebView(Window window, string url)
        {
            try
            {
                var clickTime = ApiProvider.Instance.TimeSpanMilliseconds;
                if (IsNavigate(StaticValue.WEB_NAME, clickTime))
                {
                    if (PlantformUntils.System == Platforms.Linux)
                        $"xdg-open {url}".ExecuteShell();
                    else
                        $"start {url}".ExecuteShell();
                    lastClickTime = clickTime;
                }
                else
                    lastClickTime = clickTime;
            }
            catch { }
        }
        public static void OpenWebView(string url, bool isArticle = false)
        {
            try
            {
                var clickTime = ApiProvider.Instance.TimeSpanMilliseconds;
                if (IsNavigate(nameof(StaticValue.WEB_NAME), clickTime))
                {
                    if (isArticle)
                    {
                        url = url.Replace("www.bilibili.com/read/cv", "www.bilibili.com/read/mobile/");
                        if (!url.Contains("bilibili.com"))
                            url = "https://www.bilibili.com/read/mobile/" + url;
                    }
                    if (PlantformUntils.System == Platforms.Linux)
                        $"xdg-open {url}".ExecuteShell();
                    else
                        $"start {url}".ExecuteShell();
                    lastClickTime = clickTime;
                }
                else
                    lastClickTime = clickTime;
            }
            catch { }
        }
        public static T OpenWindow<T>(object tag = null) where T : WindowBase
        {
            try
            {
                var clickTime = ApiProvider.Instance.TimeSpanMilliseconds;
                var type = typeof(T);
                if (IsNavigate(type.Name, clickTime))
                {
                    var instance = Activator.CreateInstance(type);
                    if (instance is WindowBase window)
                    {
                        Dispatcher.UIThread.InvokeAsync(() =>
                        {
                            window.Show();
                            window.Data = tag;
                        });
                        lastClickTime = clickTime;
                        return window as T;
                    }
                    else
                        lastClickTime = clickTime;
                }
                else
                    lastClickTime = clickTime;
            }
            catch { }
            return default;
        }
        public static WindowBase OpenWindow(Type type, dynamic tag = null)
        {
            try
            {
                var clickTime = ApiProvider.Instance.TimeSpanMilliseconds;
                if (IsNavigate(type.Name, clickTime))
                {
                    var instance = Activator.CreateInstance(type);
                    if (instance is WindowBase window)
                    {
                        Dispatcher.UIThread.InvokeAsync(() =>
                        {
                            window.Show();
                            if (tag != null)
                                window.Data = tag;
                        });
                        lastClickTime = clickTime;
                        return window;
                    }
                    else
                        lastClickTime = clickTime;
                }
                else
                    lastClickTime = clickTime;
            }
            catch { }
            return default;
        }
    }
}